<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    v-if="dialogVisible"
    width="36.4%"
    :close-on-click-modal="false"
    :before-close="handleClose"
    class="add-dialog"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="系统名称：" prop="name">
          <el-input
            size="small"
            v-model="ruleForm.name"
            @keyup.enter.native="submitForm('ruleForm')"
            v-focus
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button type="primary" @click="submitForm('ruleForm')" size="small"
        >确 定</el-button
      >
      <el-button @click="resetForm('ruleForm')" size="small">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      title: "新增系统",
      dialogVisible: false,
      ruleForm: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入系统名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(data) {
      this.title = "新增系统";
      if (data) {
        console.log(data);
        this.title = "编辑系统";
        this.ruleForm = { ...data };
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
      // this.dialogVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("save", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        name: "",
      };

      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields();
      }
      // this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
  },
  created() {},
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}

</style>
